<?php
require_once('RongImApi.php');
class RongImManager extends CI_Model {
	private $rongApi;
    public function __construct()
    {
        $this->load->database();
		$this->rongApi=new RongImApi();
		$this->load->model('Util');
		$this->load->model('UserManager');
		$this->load->model('DatabaseManager');
		$this->load->model('SessionManager');
    }
	public function get_token($username,$sessionId){
		if(!$this->SessionManager->check($username,$sessionId)){
			return $this->Util->format_return(0,6,'wrong session');
		}
		return $this->get_token_by_username($username);
	}
	public function get_token_by_username($username){
		$userData=$this->DatabaseManager->get_user_info($username);
		$token=$this->rongApi->getToken($username,$userData['name'],$userData['avatarUrl']);
		$token=json_decode($token);
		if($token->code==200){
			$this->DatabaseManager->save_token($username,$token->token);
			return $this->Util->format_return(1,1,'success',$token->token);
		}else{
			return $this->Util->format_return(0,8,'error',$token->msg);
		}
	}
	public function refresh_user($username,$name,$url){
		 $this->rongApi->userRefresh($username,$name,url);
	}
}